
/**
 * loads the table for galeries
 */
function loadGaleryTable(){
	pageSettings.saveInfo.moduleInfo.name = 'GALLERY'; 
	var target = 'content';
	var params = getEncodedParams({'T':'GALERY'});
	loadHtmlContent(target, loadGalleryGridUrl, params)
}


/**
 * loads a new form for creating a galery
 */
function handleNewGalery(){
	var target = 'content';
	var params = getEncodedParams({'T':'NEW'});
	loadHtmlContent(target, loadGalleryFormUrl, params, initGallery)
}


/**
 * loads the form with values for existing gallery entry
 * @param 		int		galleryId
 */
function editGallery(galleryId){
	var target = 'content';
	var params = getEncodedParams({'T':'EDIT', 'GALLERY_ID':galleryId});
	loadHtmlContent(target, loadGalleryFormUrl, params, initGallery)
}


/**
 * loads the form with values for given image
 * @param		int		 imageId
 */
function editImage(imageId){
	pageSettings.saveInfo.moduleInfo.name = 'IMAGE';
	setRecordId(imageId);
	var target = 'content';
	var params = getEncodedParams({'T':'IMAGE', 'IMAGE_ID':imageId});
	loadHtmlContent(target, loadGalleryFormUrl, params, initImage);
}


/**
 * saves the gallery
 * @param		int		galleryId
 */
function saveGallery(galleryId){
	if (checkGalleryInformation()){
		var params = $('#gallery_de').getFormParams() + '&' + $('#gallery_en').getFormParams();
		if (galleryId == null)
			params += '&' + getEncodedParams({'T':'NEW'});
		else
			params += '&' + getEncodedParams({'T':'EDIT', 'GALLERY_ID':galleryId});
		
		$.ajax({
			url: loadGalleryDmlUrl,
			type: 'POST',
			dataType: 'json',
			data: params,
			success: function(response){
				if (response != null){
					if (response.SUCCESS == 1){
						setStatus(response.SUCCESS_MSG);
						setRecordId(response.GALLERY_ID);
						editGallery(response.GALLERY_ID);
					} else
						setStatus(response.ERROR_MGS, true);
				}
			}
		});
	}
}


/**
 * saves the metainformation if given image
 * @param 		int		imageId
 */
function saveImage(imageId){
	if (checkImageInformation()){
		var params = $('#image_de').getFormParams() + '&' + $('#image_en').getFormParams();
		params += '&' + getEncodedParams({'T':'IMAGE', 'IMAGE_ID':imageId});
		
		$.ajax({
			url: loadGalleryDmlUrl,
			type: 'POST',
			dataType: 'json',
			data: params,
			success: function(response){
				if (response != null){
					if (response.SUCCESS == 1){
						setStatus(response.SUCCESS_MSG);
						setRecordId(response.IMAGE_ID);
						//editGallery(response.GALLERY_ID);
					} else
						setStatus(response.ERROR_MGS, true);
				}
			}
		});
	}
}


/**
 * checks the metainformation of gallery
 * @return		bool
 */
function checkGalleryInformation(){
	var isValid = checkGalleryInformationByLanguage('de');
	if (isValid)
		isValid = checkGalleryInformationByLanguage('en');
	return isValid;
}


/**
 * checks all fields by given language
 * @param 		string		lang
 * @return		bool		isValid
 */
function checkGalleryInformationByLanguage(lang){
	var isValid = true;
	var language = (lang.toUpperCase() == 'DE') ? 'Deutsch' : 'Englisch';
	
	if (isValid&&document.getElementById('gallery_name_' + lang)&& !(isValid= !isNull(document.getElementById('gallery_name_' + lang))))
		setStatus('Name-' + language + ' nicht gesetzt!', true);
	return isValid;
}


/**
 * checks the metainformation of image
 * @returns 	bool	
 */
function checkImageInformation(){
	var isValid = checkImageInformationByLanguage('de');
	if (isValid)
		isValid = checkImageInformationByLanguage('en');
	
	var width = parseInt($('#image_width_de').val());
	var height = parseInt($('#image_height_de').val());
	
	if (width == null || isNaN(width)){
		isValid = false;
		setStatus('Breite muss eine ganze Zahl sein!', true);
	}
	
	if (height == null || isNaN(height)){
		isValid = false;
		setStatus('H&ouml;he muss eine ganze Zahl sein!', true);
	}
	return isValid;
}


/**
 * checks all fields by given language
 * @param 		string		lang
 * @return		bool		isValid
 */
function checkImageInformationByLanguage(lang){
	var isValid = true;
	var language = (lang.toUpperCase() == 'DE') ? 'Deutsch' : 'Englisch';
	
	if (isValid&&document.getElementById('image_name_' + lang)&& !(isValid= !isNull(document.getElementById('image_name_' + lang))))
		setStatus('Name-' + language + ' nicht gesetzt!', true);
	return isValid;
}


/**
 * generates the tab structe, initialises the editor and set
 * readonly attribute for english display dates
 */
function initGallery(){
	$('#galleryTabs ul').idTabs();
	$('#gallery_active_en').attr('disabled', 'disabled');
	$('#gallery_active_de').change(copyGalleryActiveState);
	
	$('#gallery_image_preview img').load(function(){
		$(this).parent().children('div').hide();
		$(this).fadeIn(1000);
	});
}


/**
 * generates the tab structure for images
 */
function initImage(){
	$('#imageTabs ul').idTabs();
	$('#image_active_en').attr('disabled', 'disabled');
	$('#image_width_en').attr('disabled', 'disabled');
	$('#image_height_en').attr('disabled', 'disabled');
	$('#image_active_de').change(copyImageActiveState);
	$('#image_height_de').change(function(){
		copyText('image_height_en', $(this).val());
	});
	$('#image_width_de').change(function(){
		copyText('image_width_en', $(this).val());
	});
}


/**
 * copies the active status of german gallery to english gallery
 */
function copyGalleryActiveState(){
	copyActiveState('gallery');
}

/**
* copies the active status of german image to english image
*/
function copyImageActiveState(){
	copyActiveState('image');
}


/**
* copies the active status to given target
* @param		string		target
*/
function copyActiveState(target){
	var checkState = $('#' + target + '_active_de').attr('checked');
	if (checkState == 'checked')
		$('#' + target + '_active_en').attr('checked', 'checked');
	else
		$('#' + target + '_active_en').removeAttr('checked');
}


/**
 * shows the section for upload an image
 * and hides the button for showing
 */
function showUploadSection(){
	$('#uploadFields').show();
	$('#uploadFormButton').hide();
}


/**
 * initializes the ajax upload
 */
function initUpload(){
	if ($('#fileName').val() != ''){
		$('#uploadForm').ajaxForm({
			complete: finishUpload,
			uploadProgress: setUploadProgress
		}); 
		
		$('#uploadForm').submit();
	} else 
		setStatus('Keine Datei ausgew&auml;hlt!', true);
}


/**
 * opens the form for editing the image
 * @param 		mixed		xhr		xml-http-request
 */
function finishUpload(xhr){
	response = jQuery.parseJSON(xhr.responseText);
	if (response != null && response.SUCCESS == 1){
		if (response.IMAGE_ID != null)
			editImage(response.IMAGE_ID);
	} else if (response.MSG != null)
		setStatus(response.MSG, true);
}


/**
 * sets the progress for the upload
 * @param 		mixed		event
 * @param 		mixed		position
 * @param 		float		total
 * @param 		float		percentComplete
 */
function setUploadProgress(event, position, total, percentComplete){
    var percentVal = percentComplete + '%';
    $('#uploadProgress').html(percentVal);
}



function deleteImg(imageId, fileId, galleryId){
	var params = {'T':'DELETE', 'IMAGE_ID':imageId, 'FILE_ID':fileId};
	params = getEncodedParams(params);
	
	$.ajax({
		url: loadGalleryFileUrl,
		type: 'POST',
		dataType: 'json',
		data: params,
		success: function(response){
			setStatus('Image erfolgreich gel&ouml;scht');
			editGallery(galleryId);
		}
	});
}


/**
 * scales the values for the image sizes
 * @param 		bool		bigger
 */
function scaleImg(bigger){
	var percent = prompt('Geben Sie einen Wert(%) an, um welcher das Bild skaliert werden soll!');
	if (!isNaN(percent) && percent >= 0 && percent <= 100){
		var width = parseInt($('#image_width_de').val());
		var height = parseInt($('#image_height_de').val());
		
		if (bigger){
			width = width + (width * (percent/100));
			height += (height * (percent/100));
		} else {
			width -= (width * (percent/100));
			height -= (height * (percent/100));
		}

		$('#image_width_de').val(Math.round(width));
		$('#image_height_de').val(Math.round(height));
	}
}



















